package db;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Manages the users table and users objects
 * @author brook
 */
public class UserDAO implements Serializable {
    private static final String LOGIN = "SELECT * from Users where email=? and password=?";
    private static final String CREATEUSER = "INSERT into Users "
            + "(email, firstname, lastname, password, authority) values " 
            + "(?, ?, ?, ?, ?)";  
    private static final String CREATEEMPLOYEE = "INSERT into Users "
            + "(firstname, lastname,email, password,startDate,hourlyRate,address,ssn,authority) values " 
            + "(?, ?, ?, ?, ?,?,?,?,?)";  
    public int id;
    public String email;
    public double rating;
    public String creditCardNumber;
    public String lastname;
    public String firstname;
    public String address;
    public String password;
    public int authority;
    public String ssn;
    public double hourlyRate;
    public String startDate;
    public int level;
    
    
    //User type enum 
    enum Authority{
        ADMIN, USER
    }
    
    public static UserDAO loginUser(String email, String password){
        try {
            Connection connection = DB.getConnection();
            PreparedStatement ps = connection.prepareStatement(LOGIN);
            ps.setString(1, email);
            ps.setString(2, password);
            
            ResultSet rs = ps.executeQuery();
            if(rs.first()){
               return extractUser(rs);
            }             
        } catch(Exception e){
            e.printStackTrace();
        }
        return null;        
    }
    
    public static UserDAO createUser(String email, String first, String last, String password, int authority){
        try {
            Connection connection = DB.getConnection();
            PreparedStatement ps = connection.prepareStatement(CREATEUSER, Statement.RETURN_GENERATED_KEYS);
            ps.setString(1, email);
            ps.setString(2, first);
            ps.setString(3, last);
            ps.setString(4, password);
            
            ps.setInt(5,authority);
            int result = ps.executeUpdate();
            ResultSet rs = ps.getGeneratedKeys();
            if(rs.next()){
               UserDAO user = new UserDAO();
               user.setEmail(email);
               user.setFirstname(first);
               user.setLastname(last);
               user.setPassword(password);
               user.setAuthority(authority);
               return user;
            }             
        } catch(Exception e){
            e.printStackTrace();
        }
        return null;        
    }
    public static UserDAO createEmployee(String firstname, String lastname,String email,String password,String startDate,double hourlyRate,String address,String ssn, int authority){
        try {
            Connection connection = DB.getConnection();
            PreparedStatement ps = connection.prepareStatement(CREATEEMPLOYEE, Statement.RETURN_GENERATED_KEYS);
            ps.setString(1, firstname);
            ps.setString(2, lastname);
            ps.setString(3, email);
            ps.setString(4, password);
            ps.setString(5, startDate);
            ps.setDouble(6, hourlyRate);
            ps.setString(7, address);
            ps.setString(8, ssn);
            ps.setInt(9,authority);
            int result = ps.executeUpdate();
            ResultSet rs = ps.getGeneratedKeys();
            if(rs.next()){
               UserDAO employee = new UserDAO();
               employee.setEmail(email);
               employee.setFirstname(firstname);
               employee.setLastname(lastname);
               employee.setPassword(password);
               employee.setAddress(address);
               employee.setHourlyRate(hourlyRate);
               employee.setStartDate(startDate);
               employee.setSsn(ssn);
               employee.setAuthority(authority);
               return employee;
            }             
        } catch(Exception e){
            e.printStackTrace();
        }
        return null;   
    }
    /**
     * Extract the user from the result set
     * @param rs
     * @return
     * @throws SQLException 
     */
    public static UserDAO extractUser(ResultSet rs) throws SQLException{
        
        String email = rs.getString("Email");
        //Double rating = rs.getDouble("Rating");
        //String CreditCardNumber = rs.getString("CreditCardNumber");
        String lastname = rs.getString("LastName");
        String firstname = rs.getString("FirstName");
        //String address = rs.getString("Address");
        String password = rs.getString("Password");
        int authority = rs.getInt("Authority");
        //String ssn = rs.getString("SSN");
        //Double hourlyRate = rs.getDouble("HorulyRate");
        //String startDate = rs.getString("StartDate");
        //int level = rs.getInt("Level");
        
        
        
        UserDAO result = new UserDAO();
        result.setEmail(email);
        //result.setRating(rating);
        //result.setCreditCardNumber(CreditCardNumber);
        result.setLastname(lastname);
        result.setFirstname(firstname);
        //result.setAddress(address);
        result.setPassword(password);
        result.setAuthority(authority);
        //result.setSsn(ssn);
        //result.setHourlyRate(hourlyRate);
        //result.setStartDate(startDate);
        //result.setLevel(level);
        
        return  result;
    }
    
    /*public UserDAO(String email, String firstname, String lastname, String password, int authority) {
        this.email = email;
        this.firstname = firstname;
        this.lastname = lastname;
        this.password = password;
        this.authority = authority;
    }*/
    public UserDAO(){}
    
    public void SetEmployee(String firstname, String lastname,String email,String password,String startDate,double hourlyRate,String address,String ssn, int authority){
        this.email = email;
        this.firstname = firstname;
        this.lastname = lastname;
        this.password = password;
        this.startDate =startDate;
        this.hourlyRate = hourlyRate;
        this.address = address;
        this.ssn = ssn;
        this.authority = authority;
    }
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public double getRating() {
        return rating;
    }

    public void setRating(double rating) {
        this.rating = rating;
    }

    public String getCreditCardNumber() {
        return creditCardNumber;
    }

    public void setCreditCardNumber(String creditCardNumber) {
        this.creditCardNumber = creditCardNumber;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getAuthority() {
        return authority;
    }

    public void setAuthority(int authority) {
        this.authority = authority;
    }

    public String getSsn() {
        return ssn;
    }

    public void setSsn(String ssn) {
        this.ssn = ssn;
    }

    public double getHourlyRate() {
        return hourlyRate;
    }

    public void setHourlyRate(double hourlyRate) {
        this.hourlyRate = hourlyRate;
    }

    public String getStartDate() {
        return startDate;
    }

    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    
    
    
}
